package com.example.myapplication.Adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ShareActionProvider;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.myapplication.Bean.Msg;
import com.example.myapplication.R;

import java.util.List;

public class MsgAdapter  extends ArrayAdapter<Msg> {

    int resourceid;

    public MsgAdapter(@NonNull Context context, int resource, @NonNull List<Msg> objects) {
        super(context, resource, objects);
        this.resourceid=resource ;//用于接住ID

    }
    public  View getView(int position, View converView, ViewGroup parent){
        View view =converView;
        if (view==null){
            view= LayoutInflater.from(getContext()).inflate(resourceid,null);

        }
        LinearLayout left_layout=view.findViewById(R.id.left_layout);
        LinearLayout right_layout=view.findViewById(R.id.right_layout);
        TextView left_msg=view.findViewById(R.id.left_msg);
        TextView right_msg=view.findViewById(R.id.right_msg);
        SharedPreferences sp=getContext().getSharedPreferences("userinfo",Context.MODE_PRIVATE);
        int myid=sp.getInt("myid",-1);
        Msg msg =getItem(position);
        if (myid==msg.getSend_id()){
            /*如果获取的id是登陆者的id那么发送消息是在右边，如果是接收者id，则消息框是左边*/
            left_layout.setVisibility(View.GONE);//不可见，不占空间
            right_layout.setVisibility(View.VISIBLE);//可见
            right_msg.setText(msg.getContent());//右边发送的内容

        }else {
            right_layout.setVisibility(View.GONE);
            left_layout.setVisibility(View.VISIBLE);
            left_msg.setText(msg.getContent());//左边发送的内容

        }
        return view;
    }
}
